package mr;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapThread extends Thread{
    private File file;//接收具体的哪一个文件
    private int flag;//文件编号

    public MapThread(File file, int flag) {
        this.file = file;
        this.flag = flag;
    }

    @Override
    public void run() {

        try {
            BufferedReader br =
                    new BufferedReader(new FileReader(file));

            HashMap<String, Integer> map = new HashMap<>();
            String line;
            while ((line=br.readLine())!=null){

                String clazz = line.split(",")[4];

                if (!map.containsKey(clazz)){

                    map.put(clazz,1);
                }else {
                    map.put(clazz,map.get(clazz)+1);
                }

            }
            br.close();
            BufferedWriter bw = new BufferedWriter
                    (new FileWriter("F:\\数加\\代码\\nanshan\\part\\part---"+flag));

            Set<Map.Entry<String, Integer>> entries = map.entrySet();

            for (Map.Entry<String, Integer> entry : entries) {

                String clazz = entry.getKey();
                Integer value = entry.getValue();
                bw.write(clazz+":"+value);
                bw.newLine();

            }
            bw.flush();
            bw.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
